package cn.edu.njnu.opengmpback.collaboration.dao.TaskDependency;

import cn.edu.njnu.opengmpback.collaboration.entity.TaskDependencyGraph;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public class TaskDependencyDaoImpl implements TaskDependencyDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public TaskDependencyGraph save(TaskDependencyGraph graph) {
        // 如果是更新，设置更新时间
        if (graph.getId() != null) {
            graph.setUpdatedTime(new Date());
        }
        return mongoTemplate.save(graph);
    }

    @Override
    public TaskDependencyGraph findByProjectId(String projectId) {
        Query query = new Query(Criteria.where("projectId").is(projectId));
        return mongoTemplate.findOne(query, TaskDependencyGraph.class);
    }

    @Override
    public void deleteByProjectId(String projectId) {
        Query query = new Query(Criteria.where("projectId").is(projectId));
        mongoTemplate.remove(query, TaskDependencyGraph.class);
    }
}